<?php

namespace App\Http\Controllers\WorkOrder\Web;

use App\Http\Controllers\BaseController;
use App\Http\Validate\AbstractBaseValidate;
use App\Models\WorkOrder\ProductSelectionProposal\WorkOrderProductSelectionProposalModel;
use App\Service\WorkOrder\ProductSelectionProposal\ProductSelectionProposalService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ProductSelectionProposalWebController extends BaseController
{


    /**
     * 选品建议列表
     * @param Request $request
     * @return JsonResponse
     */
    public function lists(Request $request): JsonResponse
    {
        $data['startTime']   = (string)$request->get('startTime', '');
        $data['endTime']     = (string)$request->get('endTime', '');
        $data['name']        = (string)$request->get('name', '');       // 门店编号/门店名称/手机号/提报人
        $data['productName'] = (string)$request->get('productName', '');// 产品名称/品牌
        $data['status']      = (string)$request->get('status', '');     // 采纳状态 1-评审中 2-已采纳 3-未采纳 4-上线 5-爆品
        $data['dealStatus']  = (string)$request->get('dealStatus', ''); // 处理状态 1-未回复 2-已回复
        $pageNum             = (int)$request->get('pageNum', 1);
        $pageSize            = (int)$request->get('pageSize', 20);

        if ($data['startTime']) {
            $data['startTime'] = strtotime($data['startTime']);
        } else {
            $data['startTime'] = strtotime(date('Y-m-d'));
        }
        if ($data['endTime']) {
            $data['endTime'] = strtotime($data['endTime'] . ' 23:59:59');
        } else {
            $data['endTime'] = strtotime(date('Y-m-d 23:59:59'));
        }
        if ($data['status']) {
            $data['status'] = explode(',', $data['status']);
        } else {
            $data['status'] = [];
        }
        if ($data['dealStatus']) {
            $data['dealStatus'] = explode(',', $data['dealStatus']);
        } else {
            $data['dealStatus'] = [];
        }

        return $this->success(ProductSelectionProposalService::getLists($data, $pageNum, $pageSize));
    }


    /**
     * 通过工单号获取详情
     * @param Request $request
     * @param string $workOrderNo
     * @return JsonResponse
     */
    public function detail(Request $request, string $workOrderNo): JsonResponse
    {
        return $this->success(ProductSelectionProposalService::detail($workOrderNo, 0, true));
    }

    /**
     * 通过工单号修改状态
     * @param Request $request
     * @param string $workOrderNo
     * @return JsonResponse
     */
    public function update(Request $request, string $workOrderNo): JsonResponse
    {
        $this->validate(
            $request, [
                'status'  => AbstractBaseValidate::intInValidateBuild(
                    false,
                    WorkOrderProductSelectionProposalModel::ADOPT_STATUS_TEXT
                ),
                'content' => 'required|string',
            ]
        );
        $status  = (int)$request->get('status', 0);
        $content = (string)$request->get('content', '');

        $userId   = getRmsUserId();
        $userName = getRmsUserInfoByAuth()->username ?? '';

        ProductSelectionProposalService::updateStatus($workOrderNo, $userId, $userName, $status, $content);
        return $this->success();
    }
}
